﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class Issue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.GetCurrentUser() == null)
            Response.Redirect("~/Login/LoginForm.aspx?returnUrl=" + Request.Url.PathAndQuery);
        else
            if (Session.GetCurrentUser() != "Admin")
                Response.Redirect("~/Login/Permission.aspx");
    }
    protected void ButtonIssue_Click(object sender, EventArgs e)
    {
        SqlConnection con = null;

        string sql = @"insert into Issue(BookID, UserName, IssueDate, ReturnDate, RenewTime)
                       values(@BookID, @UserName, @IssueDate, @ReturnDate, @RenewTime)";

        string url = ConfigurationManager.ConnectionStrings["LMSConnectionString"].ToString( );

        try
        {
            con = new SqlConnection(url);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);

            SqlParameter para = new SqlParameter("@BookID", System.Data.SqlDbType.Int);
            para.Value = TextBoxID.Text;
            cmd.Parameters.Add(para);

            para = new SqlParameter("@UserName", System.Data.SqlDbType.NChar, 10);
            para.Value = TextBoxUser.Text;
            cmd.Parameters.Add(para);

            para = new SqlParameter("@IssueDate", System.Data.SqlDbType.Date);
            para.Value = DateTime.Now.ToShortDateString();
            cmd.Parameters.Add(para);

            para = new SqlParameter("@ReturnDate", System.Data.SqlDbType.Date);
            para.Value = DateTime.Now.AddDays(14).ToShortDateString();
            cmd.Parameters.Add(para);

            para = new SqlParameter("@RenewTime", System.Data.SqlDbType.Int);
            para.Value = "4";
            cmd.Parameters.Add(para);

            cmd.ExecuteNonQuery();
            LabelTest.Text = "Issue book successful !";



        }
        catch
        {

            LabelTest.Text = "Check your input!";
        }
        finally
        {
            TextBoxID.Text = TextBoxUser.Text = "";
            con.Close();
        }

    }
}